var ConsultantInfoDlg = {consultantInfoData: {}, zTreeInstance: null, validateFields: {}};
ConsultantInfoDlg.clearData = function () {
    this.consultantInfoData = {};
};
ConsultantInfoDlg.set = function (key, val) {
    this.consultantInfoData[key] = (typeof value == "undefined") ? $("#" + key).val() : value;
    return this;
};
ConsultantInfoDlg.get = function (key) {
    return $("#" + key).val();
};
ConsultantInfoDlg.close = function () {
    parent.layer.close(parent.layer.getFrameIndex(window.name));
};
modifyAppointment = function (ids) {
    var index = layer.open({
        type: 2,
        title: '修改预约客户',
        area: ['70%', '95%'],
        fix: true,
        maxmin: true,
        content: Feng.ctxPath + '/consultant/doctor_addTasks/' + ids
    });
    this.layerIndex = index;
};
cancelAppointment = function (customerId) {
    $.ajax({
        url: Feng.ctxPath + "/customer/getCustomerIsDealStatus/" + customerId,
        type: "post",
        success: function (data) {
            if (data == 1) {
                alert("此患者已开单不能取消预约！");
                return
            } else if (data == 4) {
                alert("此患者充了储值或积分不能取消预约！");
                return
            } else {
                var index = layer.open({
                    type: 2,
                    title: '取消预约',
                    area: ['30%', '40%'],
                    fix: true,
                    maxmin: true,
                    content: Feng.ctxPath + '/doctorLooked/cancelAppointment/' + customerId
                });
                this.layerIndex = index;
            }
        },
        error: function (data) {
            alert('没有权限，请联系系统管理员!');
            parent.layer.close(parent.layer.getFrameIndex(window.name));
        }
    })
};
ConsultantInfoDlg.showConsultantSelectTree = function () {
    var pName = $("#pName");
    var pNameOffset = $("#pName").offset();
    $("#parentConsultantMenu").css({
        left: pNameOffset.left + "px",
        top: pNameOffset.top + pName.outerHeight() + "px"
    }).slideDown("fast");
    $("body").bind("mousedown", onBodyDown);
};
ConsultantInfoDlg.hideConsultantSelectTree = function () {
    $("#parentConsultantMenu").fadeOut("fast");
    $("body").unbind("mousedown", onBodyDown);
};
ConsultantInfoDlg.collectData = function () {
    this.set('id').set('supplementOfPayment').set('expenditureProject').set('appointmentTime').set('appointmentTime2').set('customerId').set('status').set('statuss').set('followStatus').set('followStatus2').set('consumptionStatus2').set('consumptionStatus').set('reservationsId').set('totleFoldingPay').set('diagnosisContent').set('amountOfPayment').set('operationTime').set('revisitContent').set('revisitStatus1').set('isReturnVisit').set('reservations').set('remarksOfReturnVisit').set('causeOfNoReturnVisit').set('followStatus1').set('consumptionStatus1');
};
ConsultantInfoDlg.validate = function () {
    $('#consultantInfoForm').bootstrapValidator('validate');
    return $("#consultantInfoForm").data('bootstrapValidator').isValid();
};
ConsultantInfoDlg.changeTreatmentUnit = function () {
    var treatmentUnit = $("#treatmentUnit").val();
    var customerId = $('#customerId').val();
    var tc = treatmentUnit + "," + customerId;
    var index = layer.open({
        type: 2,
        title: '变更科室',
        area: ['60%', '50%'],
        fix: true,
        maxmin: true,
        content: Feng.ctxPath + '/consultantTwo/change_treatmentUnit/' + tc
    });
    this.layerIndex = index;
};
ConsultantInfoDlg.subsequent = function (id) {
    var index = layer.open({
        type: 2,
        title: '复诊预约',
        area: ['70%', '95%'],
        fix: true,
        maxmin: true,
        content: Feng.ctxPath + '/doctorLooked/subsequent_info/' + id + '/2'
    });
    this.layerIndex = index;
};

function onBodyDown(event) {
    if (!(event.target.id == "menuBtn" || event.target.id == "parentConsultantMenu" || $(event.target).parents("#parentConsultantMenu").length > 0)) {
        ConsultantInfoDlg.hideConsultantSelectTree();
    }
};
ConsultantInfoDlg.flush = function () {
    var id = $("#customerId").val();
    var tabName = "";
    for (var i = 0; i < 9; i++) {
        if ($("#navBox").children('li')[i].className == "active") {
            var li = $("#navBox").children('li')[i];
            var a = $(li).children()[0];
            tabName = $(a).html();
            break;
        }
    }
    if (tabName == "患者信息") {
        location.href = Feng.ctxPath + '/consultantDetails/consultantToDetailByTab/' + id + '/0/0';
    } else if (tabName == "市场患者信息") {
        location.href = Feng.ctxPath + '/consultantDetails/consultantToDetailByTab/' + id + '/1/0';
    } else if (tabName == "开单操作") {
        location.href = Feng.ctxPath + '/consultantDetails/consultantToDetailByTab/' + id + '/2/0';
    } else if (tabName == "开单记录") {
        location.href = Feng.ctxPath + '/consultantDetails/consultantToDetailByTab/' + id + '/3/0';
    } else if (tabName == "看诊/回访") {
        location.href = Feng.ctxPath + '/consultantDetails/consultantToDetailByTab/' + id + '/4/0';
    } else if (tabName == "回访记录") {
        var isKz = $("#isKz").val();
        if ("0" == isKz) {
            location.href = Feng.ctxPath + '/consultantDetails/consultantToDetailByTab/' + id + '/9/0';
        } else {
            location.href = Feng.ctxPath + '/consultantDetails/consultantToDetailByTab/' + id + '/9/1';
        }
    } else if (tabName == "就诊记录") {
        location.href = Feng.ctxPath + '/consultantDetails/consultantToDetailByTab/' + id + '/5/0';
    } else if (tabName == "影像") {
        location.href = Feng.ctxPath + '/consultantDetails/consultantToDetailByTab/' + id + '/6/0';
    } else if (tabName == "病历") {
        location.href = Feng.ctxPath + '/consultantDetails/consultantToDetailByTab/' + id + '/7/0';
    } else if (tabName == "收费记录") {
        location.href = Feng.ctxPath + '/consultantDetails/consultantToDetailByTab/' + id + '/8/0';
    } else if (tabName == "会员信息") {
        location.href = Feng.ctxPath + '/consultantDetails/consultantToDetailByTab/' + id + '/10/0';
    } else {
        location.href = Feng.ctxPath + '/consultantDetails/consultantToDetailByTab/' + id + '/10/0';
    }
};
var count1 = 0;
ConsultantInfoDlg.addIsRevisit = function () {
    var id = $("#customerId").val();
    var isKz = $("#isKz").val();
    var currentSeat = parent.parent.$('#currentSeat').html();
    this.clearData();
    this.collectData();
    if (!this.validate()) {
        count1 = 0;
        return;
    }
    if (document.getElementById("reservations").value == null || document.getElementById("reservations").value == "") {
        alert("请输入回访人");
        count1 = 0;
        return;
    }
    if (document.getElementById("appointmentTime").value == null || document.getElementById("appointmentTime").value == "") {
        alert("请输入下次跟进时间");
        count1 = 0;
        return;
    }
    var followStatus = $("#followStatus1").val();
    var consumptionStatus = $("#consumptionStatus1").val();
    var revisitStatus = $("#revisitStatus1").val();
    var appointmentTime = $("#appointmentTime").val();
    var date = new Date();
    var currentYear = date.getFullYear();
    var currentMonth = date.getMonth() + 1;
    var currentDay = date.getDate();
    if (currentMonth < 10) currentMonth = "0" + currentMonth;
    if (currentDay < 10) currentDay = "0" + currentDay;
    var nowTime = currentYear + "-" + currentMonth + "-" + currentDay;
    if (appointmentTime.substring(0, 10) < nowTime && (followStatus == "" || followStatus == null || followStatus == "跟踪中")) {
        alert("下次跟进时间不能小于今天！");
        count1 = 0;
        return;
    }
    if (followStatus == null || followStatus == "" || followStatus == "请选择") {
        alert("请选择跟踪状态!");
        count1 = 0;
        return;
    }
    if (consumptionStatus == null || consumptionStatus == "" || consumptionStatus == "请选择") {
        alert("请选择消费状态!");
        count1 = 0;
        return;
    }
    if (revisitStatus == null || revisitStatus == "") {
        alert("请选择回访状态!");
        count1 = 0;
        return;
    }
    var flag = $('#isNew').is(':checked');
    var consultantInfoData = this.consultantInfoData;
    if (!flag) {
        consultantInfoData['isNew'] = "0";
        if ($('#revisitContent').val() == "" || $('#revisitContent').val() == null) {
            alert("添加回访详情不能为空");
            count1 = 0;
            return;
        }
    } else {
        consultantInfoData['isNew'] = "1"
    }
    if ($('#diagnosisContent').length > 1000) {
        alert("诊断内容小于1000");
        count1 = 0;
        return;
    }
    count1++;
    if (count1 == 1) {
        var ajax = new $ax(Feng.ctxPath + "/customer/add_yu_isRevisit", function (data) {
            Feng.success("操作成功!");
            if (currentSeat == 'TC &gt; TC回访') {
                window.parent.TcRevisit.table.refresh();
            } else if (currentSeat == 'TC &gt; TC看诊') {
                window.parent.Consultant.table.refresh();
            } else if (currentSeat == 'TC &gt; 回访记录') {
                window.parent.ConsultantRecord.table.refresh();
            } else if (currentSeat == 'TC &gt; 今日回访') {
                window.parent.ConsultantReserve.table.refresh();
            } else if (currentSeat == 'TC &gt; 回访详情') {
                window.parent.ReturnVisit.table.refresh();
            }
            location.href = Feng.ctxPath + '/consultantDetails/consultantToDetailByTab/' + id + '/4/' + isKz + '';
        }, function (data) {
            count1 = 0;
            alert("没有权限，请联系系统管理员！");
            location.href = Feng.ctxPath + '/consultantDetails/consultantToDetailByTab/' + id + '/4/' + isKz + '';
        });
        ajax.set(consultantInfoData);
        ajax.start();
    }
};
var count = 0;
ConsultantInfoDlg.addDiagnosisContent = function () {
    var id = $("#customerId").val();
    var reservationsId = $("#reservationsId").val();
    var doctor = $("#userName").val();
    var des = $("#diagnosisContent").val();
    if (des == "" || des == null) {
        alert("添加看诊记录不能为空");
        count = 0;
        return;
    }
    var currentSeat = parent.parent.$('#currentSeat').html();
    var diagnosisContent = des.replace(/\r\n/g, '<br/>').replace(/\n/g, '<br/>').replace(/\s/g, ' ');
    var isKz = $("#isKz").val();
    count++;
    if (count == 1) {
        $.ajax({
            url: Feng.ctxPath + "/customer/add_diagnosisContent1",
            type: "post",
            data: {id: id, reservationsId: reservationsId, doctor: doctor, diagnosisContent: diagnosisContent},
            success: function (data) {
                var isDorj=$("#isDorj").val();
                if(isDorj=='ys'){
                    Feng.success('操作成功!');
                    window.location.href = Feng.ctxPath + '/doctorDetails/doctor_seeDiagnosis/' + id + '/' + 4
                }else if(isDorj=='tc'){
                    Feng.success('操作成功!');
                    if (isKz == 0) {
                        window.parent.Consultant.table.refresh();
                    } else if (isKz == 1) {
                        window.parent.ConsultantReserve.table.refresh();
                    } else {
                        if (currentSeat == 'TC &gt; TC回访') {
                            window.parent.TcRevisit.table.refresh();
                        } else {
                            window.parent.ConsultantRecord.table.refresh();
                        }
                    }
                    location.href = Feng.ctxPath + '/consultantDetails/consultantToDetailByTab/' + id + '/3/0';
                }else{
                    window.location.reload();
                }
            },
            error: function (data) {
                count = 0;
                alert('没有权限，请联系管理员!');
            }
        })
    }
};
$(function () {
    $(".showBox").click(function () {
        $(this).parent().siblings().children('.hideBoxs').show();
        $(this).parent().siblings().css('box-shadow', '0 0 5px #666666');
    });
    $(".cancel").click(function () {
        $(this).parents('.hideBoxs').hide().parent().css('box-shadow', '0 0 0 white');
    });
    if ($("#followStatus").val() == "跟踪结束" || $("#followStatus").val() == "跟踪完成") {
        var data = new Date();
        var y = data.getFullYear();
        var m = data.getMonth() + 1;
        var d = data.getDate();
        if (m < 10) {
            m = "0" + m;
        }
        if (d < 10) {
            d = "0" + d;
        }
        var time = y + "-" + m + "-" + d;
        $("#appointmentTime").val(time);
    }
});